package ldkxingzhe.ldkxingzhedemo.menu;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SearchView;
import android.widget.ShareActionProvider;
import android.widget.TextView;

import ldkxingzhe.ldkxingzhedemo.R;
import ldkxingzhe.ldkxingzhedemo.utils.LogDemo;


/**
 * Created by liangdengke on 2015/8/20.
 */
public class OptionMenuActivity extends Activity{


    private int count = 1;
    private ShareActionProvider shareActionProvider;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        TextView textView = new TextView(this);
        textView.setText("Text For OptionMenu");
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        linearLayout.addView(textView, layoutParams);
        Button button = new Button(this);
        button.setText("Click and change the menu");
        linearLayout.addView(button, layoutParams);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count++;
                invalidateOptionsMenu();
            }
        });
        setContentView(linearLayout);

    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
/*        menu.clear();
        for(int i = 0; i < count; i++){
            menu.add("test " + i);
        }*/

        SearchView searchView = (SearchView)menu.findItem(R.id.search_view).getActionView();
        shareActionProvider = (ShareActionProvider)menu.findItem(R.id.share_action).getActionProvider();
        shareActionProvider.setShareIntent(createShareIntent());
        if(searchView != null){
            LogDemo.d("TAG","searchView is not null");
        }else{
            LogDemo.d("TAG","searchView is null");
        }
        return super.onPrepareOptionsMenu(menu);
    }

    private Intent createShareIntent() {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/*");
        shareIntent.putExtra(Intent.EXTRA_TEXT, "This is just a test");
        return shareIntent;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.d("TAG","title is " + item.getTitle().toString());
        switch (item.getItemId()){
            case R.id.item1:
                Log.d("TAG","item1");
                break;
            case R.id.item2:
                Log.d("TAG","item2");
                break;
            default:
                Log.d("TAG","error");
        }
        return super.onOptionsItemSelected(item);
    }
}
